package com.school.controller;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import com.alibaba.fastjson.JSONObject;
import com.school.entity.web.HljtClassInfo;
import com.school.entity.web.HljtGradeInfo;
import com.school.service.impl.HljtGradeClassServiceImpl;
import com.school.util.FileUploadUtil;
import com.school.util.PictureVideoUpload;
import com.school.util.RepUtil;
import com.school.util.WhetherNotToUpdate;

@Controller
@RequestMapping(value="HljtGradeClassController")
public class HljtGradeClassController {

	@Autowired
	private HljtGradeClassServiceImpl oService;
	/**
	 * 查找所有年级
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/findAllGrades")
	@ResponseBody
	public String findAllGrades(HttpServletResponse rep){
		RepUtil.repPake(rep);
		return oService.findAllGrades();
	}
	/**
	 * 添加年级
	 * @param strGrade
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/insertGrade")
	@ResponseBody
	public String insertGrade(String strGrade,HttpServletResponse rep){
		RepUtil.repPake(rep);
		return oService.insertGrade(strGrade);
	}
	/**
	 * 修改年级
	 * @param oHljtGradeInfo
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/updateGrade")
	@ResponseBody
	public String updateGrade(HljtGradeInfo oHljtGradeInfo,HttpServletResponse rep){
		RepUtil.repPake(rep);
		return oService.updateGrade(oHljtGradeInfo);
	}
	/**
	 * 删除年级
	 * @param oHljtGradeInfo
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/deleteGrade")
	@ResponseBody
	public String deleteGrade(HljtGradeInfo oHljtGradeInfo,HttpServletResponse rep){
		RepUtil.repPake(rep);
		return oService.deleteGrade(oHljtGradeInfo);
	}
	/**
	 * 查询所有班级 chenjieNo
	 * @param pageIndex
	 * @param pageSize
	 * @param strGrade
	 * @param strClass
	 * @param rep
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/findAllClasses")
	@ResponseBody
	public String findAllClasses(int pageIndex,int pageSize,String strGrade,String strClass ,HttpServletResponse rep,HttpServletRequest req){
		RepUtil.repPake(rep);
		String strPath=RepUtil.findAddress(req);
		return oService.findAllClasses(pageIndex, pageSize, strGrade, strClass,strPath);
	}
	
	
	
	/**
	 * 查询所有班级 chenjianYes
	 * @param pageIndex
	 * @param pageSize
	 * @param strGrade
	 * @param strClass
	 * @param rep
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/findAllClassesEX")
	@ResponseBody
	public JSONObject findAllClassesEX(int pageIndex,int pageSize,Integer strGrade,Integer strClass ,HttpServletResponse rep,HttpServletRequest req){
		RepUtil.repPake(rep);
		JSONObject ret=new JSONObject();
		try {
	//	String strPath=RepUtil.findAddress(req);
		HljtClassInfo hljtClassInfo = new HljtClassInfo();
		hljtClassInfo.setPageIndex(pageIndex);
		hljtClassInfo.setPageSize(pageSize);
		if(strGrade==-1) {
			hljtClassInfo.setGradeid(null);
		}else {
			hljtClassInfo.setGradeid(strGrade);
		}
		if(strClass==-1) {
			hljtClassInfo.setClassid(null);
			hljtClassInfo.setIsexcellent(null);
		}else {
			hljtClassInfo.setClassid(null);
			hljtClassInfo.setIsexcellent(strClass+"");
		}
		hljtClassInfo.setPageNum((hljtClassInfo.getPageIndex()-1)*hljtClassInfo.getPageSize());
	//	Map<String , Object> map = new HashMap<>();
	//	 map = oService.findAllClassesEX(hljtClassInfo);
		ret.put("teacherInfo", oService.findAllClassesEX(hljtClassInfo));
		} catch (Exception e) {
			e.printStackTrace();
			//jsonUtil.setFailMsg("服务器异常");
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	
	/**
	 * 添加班级
	 * @param imgFile
	 * @param oHljtClassInfo
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/insertClasses")
	@ResponseBody
	public String insertClasses( HljtClassInfo oHljtClassInfo,HttpServletResponse rep,HttpServletRequest req){
		RepUtil.repPake(rep);
		
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
 		 MultipartFile file = null;
 		if (isMultipart){ 
		    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(req, MultipartHttpServletRequest.class);
		    file = multipartRequest.getFile("imgFile");
		    if(!file.isEmpty()) {
			     try {
			    	 String imagePath  = PictureVideoUpload.savePictureVideo(file, WhetherNotToUpdate.getSaveDisk());
					oHljtClassInfo.setImagePath(imagePath);
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
			 
		    }
		}
 		
		
		return oService.insertClassInfo(oHljtClassInfo);
	}
	/**
	 * 修改班级
	 * @param imgFile
	 * @param oHljtClassInfo
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/updateClasses")
	@ResponseBody
	public String updateClasses(HljtClassInfo oHljtClassInfo,HttpServletResponse rep,HttpServletRequest req){
		RepUtil.repPake(rep);
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		 MultipartFile file = null;
		if (isMultipart){ 
		    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(req, MultipartHttpServletRequest.class);
		     file = multipartRequest.getFile("imgFile");
		     if(!file.isEmpty()) {
			     try {
			    	 String imagePath  = PictureVideoUpload.savePictureVideo(file, WhetherNotToUpdate.getSaveDisk());
					oHljtClassInfo.setImagePath(imagePath);
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
			 
		    }
		}
		return oService.updateClassInfo(oHljtClassInfo);
	}
	/**
	 * 删除班级
	 * @param strId
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/deleteClasses")
	@ResponseBody
	public String deleteClasses(String strId,HttpServletResponse rep){
		RepUtil.repPake(rep);
		return oService.deleteClassInfo(strId);
	}
}
